﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using Icoders.MarketTracker.Core.MasterData;
using Icoders.MarketTracker.Web.Lib.MasterDataDTO;

namespace Icoders.MarketTracker.Web.Lib.Response.Services.Impl
{
    public class MasterDataToDTOMapping : IMasterDataToDTOMapping
    {
        public MasterDataToDTOMapping()
        {
            SetupMapper();
        }

        private void SetupMapper()
        {
            
            Mapper.CreateMap<Country, CountryDTO>()
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
                .AfterMap(AfterMapAction);
            Mapper.CreateMap<City, CityDTO>()
               .AfterMap(AfterMapAction);
            Mapper.CreateMap<Area, AreaDTO>()
              .AfterMap(AfterMapAction);
            Mapper.CreateMap<Zone, ZoneDTO>()
              .AfterMap(AfterMapAction);
            Mapper.CreateMap<Product, ProductDTO>()
              .AfterMap(AfterMapAction);
            Mapper.CreateMap<PointOfPurchase, PointOfPurchaseDTO>()
                .ForMember(dest => dest.AreaId, opt => opt.MapFrom(src => src.Area))
                .ForMember(dest => dest.CountryId, opt => opt.MapFrom(src => src.Country))
                .ForMember(dest => dest.CityId, opt => opt.MapFrom(src => src.City))
                .ForMember(dest => dest.PointOfPurchaseTypeId, opt => opt.MapFrom(src => src.PointOfSaleTypeId))
              .AfterMap(AfterMapAction);
            Mapper.CreateMap<PointOfPurchaseType, PointOfPurchaseTypeDTO>()
              .AfterMap(AfterMapAction);
            Mapper.CreateMap<Reason, ReasonDTO>()
              .AfterMap(AfterMapAction);
            Mapper.CreateMap<Client, ClientDTO>()
              .AfterMap(AfterMapAction);
            Mapper.CreateMap<User, UserDTO>()
              .AfterMap(AfterMapAction);
        }

        void AfterMapAction(MasterEntityBase me, BaseDTO mb)
        {
            if (me == null)
            {
                //Workaround for e.g. Route which bring a null me
                return;
            }
            mb.CreatedOn = me.CreatedOn;
            mb.MasterId = me.Id;
           
        }

        public CountryDTO Map(Country country)
        {
            if (country == null) return null;
            return Mapper.Map<Country, CountryDTO>(country);
        }

        public CityDTO Map(City city)
        {
            if (city == null) return null;
            return Mapper.Map<City, CityDTO>(city);
        }

        public AreaDTO Map(Area area)
        {
            if (area == null) return null;
            return Mapper.Map<Area, AreaDTO>(area);
        }

        public ZoneDTO Map(Zone zone)
        {
            if (zone == null) return null;
            return Mapper.Map<Zone, ZoneDTO>(zone);
        }

        public PointOfPurchaseDTO Map(PointOfPurchase pop)
        {
            if (pop == null) return null;
            return Mapper.Map<PointOfPurchase, PointOfPurchaseDTO>(pop);
        }

        public PointOfPurchaseTypeDTO Map(PointOfPurchaseType popType)
        {
            if (popType == null) return null;
            return Mapper.Map<PointOfPurchaseType, PointOfPurchaseTypeDTO>(popType);
        }

        public ProductDTO Map(Product product)
        {
            if (product == null) return null;
            return Mapper.Map<Product, ProductDTO>(product);
        }

        public ClientDTO Map(Client client)
        {
            if (client == null) return null;
            return Mapper.Map<Client, ClientDTO>(client);
        }

        public ReasonDTO Map(Reason reason)
        {
            if (reason == null) return null;
            return Mapper.Map<Reason, ReasonDTO>(reason);
        }

        public UserDTO Map(User user)
        {
            if (user == null) return null;
            return Mapper.Map<User, UserDTO>(user);
        }
    }
}
